package com.cash.app.utils.extra.tools

import android.content.BroadcastReceiver
import android.os.BatteryManager
import com.cash.app.utils.TLog
import com.cash.app.utils.extra.ExtraInfoUtils
import com.cash.app.utils.extra.model.BatteryStatusData
import com.cash.app.utils.extra.tools.OtherUtils.brightness

class BatteryBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: android.content.Context, intent: android.content.Intent) {
        val batteryStatusData = BatteryStatusData()
        val action = intent.action
        if (android.content.Intent.ACTION_BATTERY_CHANGED == action) {
//            String charge_type = "";
            var charge_type = 0
            when (intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)) {
                BatteryManager.BATTERY_STATUS_CHARGING -> charge_type =
                    BatteryManager.BATTERY_STATUS_CHARGING

                BatteryManager.BATTERY_STATUS_DISCHARGING -> charge_type =
                    BatteryManager.BATTERY_STATUS_DISCHARGING

                BatteryManager.BATTERY_STATUS_NOT_CHARGING -> charge_type =
                    BatteryManager.BATTERY_STATUS_NOT_CHARGING

                BatteryManager.BATTERY_STATUS_FULL -> charge_type =
                    BatteryManager.BATTERY_STATUS_FULL

                BatteryManager.BATTERY_STATUS_UNKNOWN -> charge_type =
                    BatteryManager.BATTERY_STATUS_UNKNOWN
            }

            //            String battery_health = "";
            var battery_health = 0
            when (intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)) {
                BatteryManager.BATTERY_HEALTH_UNKNOWN -> battery_health =
                    BatteryManager.BATTERY_HEALTH_UNKNOWN

                BatteryManager.BATTERY_HEALTH_GOOD -> battery_health =
                    BatteryManager.BATTERY_HEALTH_GOOD

                BatteryManager.BATTERY_HEALTH_DEAD -> battery_health =
                    BatteryManager.BATTERY_HEALTH_DEAD

                BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> battery_health =
                    BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE

                BatteryManager.BATTERY_HEALTH_OVERHEAT -> battery_health =
                    BatteryManager.BATTERY_HEALTH_OVERHEAT
            }

            //            String status = "";
            var status = 0
            when (intent.getIntExtra("plugged", 0)) {
                BatteryManager.BATTERY_PLUGGED_AC -> //                    status = "plugged ac";
                    status = BatteryManager.BATTERY_PLUGGED_AC

                BatteryManager.BATTERY_PLUGGED_USB -> //                    status = "plugged usb";
                    status = BatteryManager.BATTERY_PLUGGED_USB

                BatteryManager.BATTERY_PLUGGED_WIRELESS -> //                    status = "plugged wireless";
                    status = BatteryManager.BATTERY_PLUGGED_WIRELESS
            }
            batteryStatusData.charge_type = status
            batteryStatusData.battery_pct = intent.getIntExtra("level", 0).toDouble()
            batteryStatusData.battery_temperature = intent.getIntExtra("temperature", 0)
            batteryStatusData.is_charging = charge_type
            batteryStatusData.battery_health = battery_health
            batteryStatusData.screen_brightness = brightness.toDouble()
            //            batteryStatusData.device_id = getDeviceid();

            ExtraInfoUtils.batteryStatusData = batteryStatusData
            TLog.d("uploadExtraHardware battery=${batteryStatusData}", "GlobalTaskManager")
        }
    }
}
